from apps.common.base.views import *
from apps.system.article.serializers import *
from apps.system.article.dao import ArticleDao
from apps.system.article.filters import ArticleFilter


class BaseArticleViewSet(BaseViewSet):
    serializer_class = ArticleModelSerializer
    filter_class = ArticleFilter
    DAO_CLASS = ArticleDao

    @action(methods=['post'], detail=False)
    def getItemById(self, request):
        params = getRequestParams(self.request)
        obj_id = params.get('obj_id')
        if not obj_id:
            return Response(response_message('obj_id参数必填'))
        obj = self.DAO_CLASS().findOneByObjId(obj_id)
        # 增加销量
        obj.hots = F('hots') + 1
        obj.save()
        # 增加阅读人数
        obj.users.add(request.user)
        obj = self.DAO_CLASS().findOneByObjId(obj_id)
        model = self.serializer_class(obj).data
        return Response(response_success(model))